package com.wbao.dianniu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.SpanTextView;
import com.wbao.dianniu.data.Childrens;
import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.utils.ActivityUtils;

import java.util.List;

/**
 * 二级回复-- 部分列表
 * <p>
 * Created by yejianjun on 2018/5/18.
 */

public class ComReplyAdapter extends BaseAdapter {

    private List<Childrens> list;
    private LayoutInflater inflater;
    private Context mContext;
    private int questId;
    private int pid;
    private int type;
    private QuestAnswerListData data;

    public ComReplyAdapter(Context context, List<Childrens> list, int questId, int pid, int type, QuestAnswerListData data) {
        this.list = list;
        this.mContext = context;
        this.questId = questId;
        this.pid = pid;
        this.type = type;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    public void addOneData(Childrens childrens) {
        if (null != list) {
            list.add(0, childrens);
            notifyDataSetChanged();
        }
    }

    public void removeOneData(int id) {
        if (null != list) {
            for (int i = 0; i < list.size(); i++) {
                if (id == list.get(i).id) {
                    list.remove(i);
                    notifyDataSetChanged();
                    break;
                }

            }
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.reply_item, null);
            holder.nameTV = (TextView) convertView.findViewById(R.id.replyName);
            holder.contentTV = (SpanTextView) convertView.findViewById(R.id.replyContent);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Childrens bean = list.get(position);
        holder.nameTV.setText(bean.name + "：");

        holder.contentTV.setEmojiText(bean.content, bean.noticeJson);
//        holder.contentTV.setText(bean.content == null ? "" : bean.content);

        CLicklistner cLicklistner = new CLicklistner(bean.accountId, questId, pid, data.anonymous);
        holder.nameTV.setOnClickListener(cLicklistner);
        holder.contentTV.setOnClickListener(cLicklistner);
        return convertView;
    }

    private class ViewHolder {
        private TextView nameTV;
        private SpanTextView contentTV;
    }


    class CLicklistner implements View.OnClickListener {

        private int accountId;
        private int questId;
        private int pid;
        private Integer anonymous;

        public CLicklistner(int accountId, int questId, int pid, Integer anonymous) {
            this.accountId = accountId;
            this.questId = questId;
            this.pid = pid;
            this.anonymous = anonymous;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.replyName:
                    if (null != data.anonymous && data.anonymous == 0) {
                        ActivityUtils.intoHeadInfoActivity(accountId, mContext);
                    }
                    break;
                case R.id.replyContent:
                    ActivityUtils.intoTwoCommentActivity(mContext, questId, pid, type, data);
                    break;
            }
        }
    }


    public void addAll(List<Childrens> list) {
        this.list = list;
        notifyDataSetChanged();
    }
}
